<?php
/**
 * Created by PhpStorm.
 * User: steven
 * Date: 2017/9/13
 * Time: 下午2:52
 */

namespace common\tools;


class Http
{
    public static function sendGet($url) {
        $ch = curl_init(); //初始化curl
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_TIMEOUT, 300);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,300);
        $response = curl_exec($ch);//接收返回信息
        if (curl_errno($ch)==28) {//出错则显示错误信息
            Logger::instance('http_curl_get')->log('超时了,请求url:'.$url);
            throw new \Exception('超时请求url:'.$url);
        }
        curl_close($ch); //关闭curl链接
        return $response;
    }

    public static function buildUrl($url,$data) {
        $info = parse_url($url);
        if (!empty($info['query'])) {
            parse_str($info['query'],$query);
            $query = array_merge($query,$data);
        } else {
            $query = $data;
        }
        Logger::debug('build_url')->log(print_r($info,1));
        $url = $info['path'];
        if (!empty($query)) {
            $url .= '?' . http_build_query($query);
        }
        return $url;
    }

    /**
     * 发送post请求
     * @param string $url 请求地址
     * @param array $post_data post键值对数据
     * @return mixed
     * @throws \Exception
     */
    public static function sendPost($url, $post_data)
    {
        $postData = http_build_query($post_data);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_AUTOREFERER,1);
        curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch,CURLOPT_HEADER,0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,30);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        $response = curl_exec($ch);//接收返回信息
        if (curl_errno($ch)==28) {//出错则显示错误信息
            Logger::instance('http_curl_post')->log('超时了,请求url:'.$url);
            throw new \Exception('超时请求url:'.$url);
        }
        curl_close($ch); //关闭curl链接
        return $response;
    }

    /**
     * 发送post请求
     * @param string $url 请求地址
     * @param array $post_data post键值对数据
     * @return mixed
     * @throws \Exception
     */
    public static function sendPostJson($url, $post_data)
    {
        $postData = json_encode($post_data);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch,CURLOPT_AUTOREFERER,1);
        curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch,CURLOPT_HEADER,0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,30);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($postData))
        );
        $response = curl_exec($ch);//接收返回信息
        if (curl_errno($ch)==28) {//出错则显示错误信息
            Logger::instance('http_curl_post')->log('超时了,请求url:'.$url);
            throw new \Exception('超时请求url:'.$url);
        }
        curl_close($ch); //关闭curl链接
        return $response;
    }

    /**
     * 获得客户端类型
     * @return int
     */
    public static function getDeviceType()
    {
        $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        $type = 3;
        if (strpos($agent, 'iphone') || strpos($agent, 'ipad')) {
            $type = 1;
        }
        if (strpos($agent, 'android')) {
            $type = 2;
        }
        return $type;
    }

    /**
     * 是否使用微信打开页面
     * @return bool
     */
    public static function isOpenByWeiXin()
    {
        $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        $type = false;
        if (strpos($agent, 'micromessenger')) {
            $type = true;
        }
        return $type;
    }

    /**
     * 打开指定类型app
     * @return string
     */
    public static function openApp(){
        $arr=array();
        $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        $type = 'other';
        if(strpos($agent, 'iphone') || strpos($agent, 'ipod') || strpos($agent, 'ipad')){
            $type = 'ios';
        }
        if(strpos($agent, 'android')){
            $type = 'android';
        }
        if (stripos($agent,'micromessenger')) {
            $arr['weixin']=true;
        }

        if(in_array($type, array('ios','android'))) {
            //$REQUEST_URI = preg_replace('@/(?:\?|$)@', '', $_SERVER['REQUEST_URI']);
            if ($type == 'ios') {
                $arr['open'] = 'wb2095945184://wealinkAPP2015://';
                $arr['downloadUrl'] = 'https://itunes.apple.com/cn/app/ruo-lin-she-jiao-zhao-pin/id635217043';
                $arr['type']='ios';
            }
            if ($type == 'android') {
                $android_package = "com.wealink.job";
                $scheme = 'vip';
                $arr['open'] = 'intent://view/#Intent;package=' . $android_package . ';scheme=' . $scheme . ';launchFlags=268435456;end;';
                $arr['downloadUrl'] = 'http://s2.wealinkcdn.com/app/wealink5.0_wealink.apk';
                $arr['type']='android';
            }

        }
        return !empty($arr)?$arr:array('open'=>'','downloadUrl'=>'','type'=>'','weixin'=>false);
    }

}